package gatech.cs3251.project4;


import java.util.ArrayList;
import java.util.List;

import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class FriendViewer extends MapActivity {
	private String logname = "FriendViewer";
	private ArrayList<LocationFormat> locations = new ArrayList<LocationFormat>();
	private List<Overlay> mapOverlays;
	private Drawable drawable;
	private FriendOverlay overlay;
	private volatile PollingThread pt;
	
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		Log.v("CHRIS", "FriendViewer activity created");
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.friend);
		
		MapView mapview;
		
		mapview = (MapView) findViewById(R.id.mapview);
		mapview.setBuiltInZoomControls(true);
		
		mapOverlays = mapview.getOverlays();
		drawable = this.getResources().getDrawable(R.drawable.androidmarker);
		overlay = new FriendOverlay(drawable);
		
		pt = new PollingThread("PollingThread");
		pt.startThread();
		
		update();
	}
	
	/**
	 * Checks the content provider for new information of users
	 */
	private void update() {
		Log.v("CHRIS", "In update");
		ArrayList<LocationFormat> locs = new ArrayList<LocationFormat>();
		
		//get from ContentProvider
		ContentResolver cr = getContentResolver();
		Cursor c = cr.query(Data.CONTENT_URI, null, null, null, null);
		Log.v("CHRIS", "Got cursor");
		if(c == null)
		{
			Log.v("CHRIS", "Cursor was null");
		}
		if(c.moveToFirst()) {
			do{
				String alt = c.getString(c.getColumnIndex(Data.altitudeCol));
				String lat = c.getString(c.getColumnIndex(Data.latitudeCol));
				String lon = c.getString(c.getColumnIndex(Data.longitudeCol));
				int userId = c.getInt(c.getColumnIndex(Data.userIdCol));
				Log.v("CHRIS", "Database got values: alt: " + alt + " lat: " +
						lat + " lon: " + lon + " id: " + userId);
				Log.v("CHRIS", "Before lf");
				LocationFormat lf = new LocationFormat(LocationFormat.sToI(lat) , LocationFormat.sToI(lon), LocationFormat.sToI(alt), userId);
				Log.v("CHRIS", "Before locs.add");
				locs.add(lf);
				Log.v("CHRIS", "End of do");
			}while(c.moveToNext());
		}
		Log.v("CHRIS", "Done iterating");
		
		//Update draw list
		for(LocationFormat l : locs) {
			Log.v("CHRIS", "Now putting down points on map");
			GeoPoint point = new GeoPoint((int)(l.getLatitudeF()*1000000),(int)(l.getLongitudeF()*1000000));
			Log.v("CHRIS", "Created point");
			OverlayItem o = new OverlayItem(point, "","");
			Log.v("CHRIS", "Created overlay item");
			overlay.addOverlay(o);
			Log.v("CHRIS", "Added overlay");
			mapOverlays.add(overlay);
			Log.v("CHRIS", "End of for loop in update");
		}
		Log.v("CHRIS", "Updated");
		locations = locs;
	}
	
	@Override
	public void onPause()
	{
		pt.stopThread();
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
    private class PollingThread implements Runnable{

		private volatile Thread runner;
		private volatile boolean go = true;
		
		public PollingThread()
		{}
		
		public PollingThread(String name)
		{
			runner = new Thread(new PollingThread(), name);
			Log.v("CHRIS", "Creating and running Viewer's PollingThread");
			runner.start();
		}
		
			public synchronized void startThread(){
			if(runner == null){
				runner = new Thread(this);
				runner.start();
			}
		}
	
		public synchronized void stopThread(){
			if(runner != null){
				Thread moribund = runner;
				runner = null;
				moribund.interrupt();
			}
		}

	    public void run()
	    {
	    	while(go)
	    	{
	    		try
	    		{
	    			Thread.sleep(1000);
	    			FriendViewer.this.update();
	    		}
	    		catch(Exception e)
	    		{
//	    			Log.e("Exception:", e.getMessage());
	    			go = false;
//	    			Log.v("CHRIS", "Interrupted");
	    		}
	    	}
	    	//Log.v("CHRIS", "Polling thread run ended");
	    }
	    
    }
}
